package com.deparse.dpframe.bean;

import com.deparse.dpframe.utils.DPGsonUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * Created by MartinDKent on 2016/1/16.
 */
public class DPObject implements Cloneable, Serializable {
    private String objectId;

    protected DPObject() {
        objectId = String.valueOf(Calendar.getInstance().getTimeInMillis() + TimeZone.getDefault().getRawOffset());
    }

    public static DPObject newInstance() {
        return new DPObject();
    }

    public String toJson() {
        return DPGsonUtil.toJson(this);
    }

    public Object clone() {
        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(this);
            out.close();
            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bin);
            Object ret = in.readObject();
            in.close();
            return ret;
        } catch (Exception e) {
            return null;
        }
    }
}
